package com.hr.user.servlet;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hr.dept.domain.Dept;
import com.hr.dept.service.Impl.DeptServiceImpl;
import com.hr.rbac.domain.Role;
import com.hr.rbac.service.RBACService;
import com.hr.user.domain.User;
import com.hr.user.service.Impl.UserServiceImpl;
import com.hr.utils.BaseServlet;
import com.hr.utils.PageBean;

public class UserServlet extends BaseServlet {
	
	private UserServiceImpl userServiceImpl = new UserServiceImpl();
	private DeptServiceImpl deptServiceImpl = new DeptServiceImpl();
	private RBACService rbacService = new RBACService();
	
	/**
	 * 实现用户退出的基础功能
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String exit(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		session.removeAttribute("user");
		return "f:/manage/login.jsp";
	}
	/**
	 * 实现用户的密码修改功能
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String updatePassword(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String password = request.getParameter("password");
		String password1 = request.getParameter("password1");
		String password2 = request.getParameter("password2");
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		if(user.getPassword().equalsIgnoreCase(password)){
			if(password1.equalsIgnoreCase(password2)){
				userServiceImpl.UpdatePassword(user, password2);
				request.setAttribute("msg", "修改成功！");
				return "f:/manage/password_alert.jsp";
			} else {
				request.setAttribute("msg", "两次密码输入不同！");
				return "f:/manage/password_alert.jsp";
			}
		} else {
			request.setAttribute("msg", "原密码不正确，请正确输入原密码！");
			return "f:/manage/password_alert.jsp";
		}
	}
	
	/**
	 * 查询所有的员工信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String queryAllEmp(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		/**
		 * 1、获取页面传递的PageCurrent
		 * 2、给定PageSize的值
		 * 3、使用PageCurrent和PageSize调用Service方法，得到PageBean，保存request域
		 * 4、转发到/admin/dept_info.jsp
		 */
		int PageCurrent = getPageCurrent(request);//获取当前页
		int PageSize = 4;//获取每页的记录数
		PageBean<User> pageBean = userServiceImpl.queryAll_Page(PageCurrent, PageSize);
		pageBean.setUrl(getUrl(request));
		request.setAttribute("pb", pageBean);
		return "f:/admin/emp_info.jsp";
	}
	/**
	 * 查询本部门的员工信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String queryAllEmpAndDept(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user = (User) request.getSession().getAttribute("user");
		int PageCurrent = getPageCurrent(request);//获取当前页
		int PageSize = 4;//获取每页的记录数
		String dept_id = user.getDept_id();
		PageBean<User> pageBean = userServiceImpl.queryAll_PageAndDept(PageCurrent, PageSize, dept_id);
		pageBean.setUrl(getUrl(request));
		request.setAttribute("pb", pageBean);
		return "f:/admin/emp_info.jsp";
	}
	/**
	 * 获取当前的页面值
	 * @param request
	 * @return
	 */
	private int getPageCurrent(HttpServletRequest request){
		String value = request.getParameter("PageCurrent");
		if(value == null || value.trim().isEmpty()){
			return 1;
		} else{
			return Integer.parseInt(value);
		}
	}
	/**
	 * 截取url
	 * @param request
	 * @return
	 */
	public String getUrl(HttpServletRequest request){
		String contextPath = request.getContextPath();//获取项目名
		String servletPath = request.getServletPath();//获取servletPath，即/CustomerServlet
		String queryString = request.getQueryString();//获取问号之后的参数部份
		
		//  判断参数部份中是否包含pc这个参数，如果包含，需要截取下去，不要这一部份。
		if(queryString.contains("&PageCurrent=")) {
			int index = queryString.lastIndexOf("&PageCurrent=");
			queryString = queryString.substring(0, index);
		}
		
		return contextPath + servletPath + "?" + queryString;
	}

	/**
	 * 添加用户前准备工作
	 * @return
	 */
	public String addPre(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		request.setAttribute("deptList", deptServiceImpl.queryALL());
		return "f:/admin/emp_add.jsp";
	}
	
	/**
	 * 展示员工的个人信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String queryUserByUserID(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		
		HttpServletRequest req = (HttpServletRequest) request;
		User user = (User) req.getSession().getAttribute("user");
		User user_find = userServiceImpl.findByUserID(user.getUser_id());
		request.setAttribute("user_find", user_find);
		
		Dept dept = deptServiceImpl.findDeptByID(user_find.getDept_id());
		user_find.setDept(dept);
		
		if(rbacService.findByUserID(user_find.getUser_id()) != null) {
			request.setAttribute("role", rbacService.findByUserID(user_find.getUser_id()));
		} else {
			Role role = null;
			request.setAttribute("role", role);
		}
		
		return "f:/admin/emp_basic_info.jsp";
	}
	
	/**
	 * 修改员工准备工作
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String UpdateUserPre(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		String user_id = request.getParameter("user_id");
		User user = userServiceImpl.findByUserID(user_id);
		request.setAttribute("user", user);
		request.setAttribute("deptList", deptServiceImpl.queryALL());
		return "f:/admin/emp_edit.jsp";
	}
	
	/**
	 * 查询具体用户详细
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String descUser(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		String user_id = request.getParameter("user_id");
		User user_find = userServiceImpl.findByUserID(user_id);
		Dept dept = deptServiceImpl.findDeptByID(user_find.getDept_id());
		user_find.setDept(dept);
		Role role = rbacService.findByUserID(user_find.getUser_id());
		request.setAttribute("user_find", user_find);
		request.setAttribute("role", role);
		return "f:/admin/emp_desc.jsp";
	}
	
	/**
	 * 删除用户
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String deleteUser(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		String user_id = request.getParameter("user_ID");
		
		if(session.getAttribute("user") != null){
			User user_session = (User) session.getAttribute("user");
			if(!user_id.equals(user_session.getUser_id())){
				User user = userServiceImpl.findByUserID(user_id);
				String Photopath = this.getServletContext().getRealPath(user.getPhoto());
				File file1 = new File(Photopath);
				if(file1.exists()){
					file1.delete();
				}
				
				String filepath  = this.getServletContext().getRealPath(user.getUserfile());
				File file2 = new File(filepath);
				if(file2.exists()){
					file2.delete();
				}
				
				userServiceImpl.deleteUserByUserID(user_id);
				request.setAttribute("msg", "工作人员=="+user.getUsername()+"==离职成功");
				return queryAllEmp(request, response);
			} else {
				request.setAttribute("msg", "您不能将自己删除");
				return queryAllEmp(request, response);
			}
		} else {
			request.setAttribute("msg", "登陆已经失效请重新登录");
			return exit(request, response);
		}
	}
	
	/**
	 * 查询当前用户的所有档案信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String queryUserFile(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		
		HttpServletRequest req = (HttpServletRequest) request;
		User user = (User) req.getSession().getAttribute("user");
		User user_find = userServiceImpl.findByUserID(user.getUser_id());
		Dept dept = deptServiceImpl.findDeptByID(user_find.getDept_id());
		user_find.setDept(dept);
		request.setAttribute("user_find", user_find);
		if(rbacService.findByUserID(user_find.getUser_id()) != null) {
			request.setAttribute("role", rbacService.findByUserID(user_find.getUser_id()));
		} else {
			Role role = null;
			request.setAttribute("role", role);
		}
		
		return "f:/admin/emp_person_record_info.jsp";
	}
	
	/**
	 * 查询前操作
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findEmpByConditionPre(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		
	    List<Dept> dept_list = deptServiceImpl.queryALL();
	    request.setAttribute("dept_list", dept_list);
	    
	    return "f:/admin/emp_find_condition.jsp";
	}
	
	/**
	 * 根据条件查询员工信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
    public String findEmpByCondition(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
    	
    	List<Dept> dept_list = deptServiceImpl.queryALL();
 	    request.setAttribute("dept_list", dept_list);
    	
    	String username = request.getParameter("username");
    	String dept_id = request.getParameter("dept_id");
    	username = new String(username.getBytes("ISO-8859-1"),"utf-8");
    	request.setAttribute("username", username);
    	int PageCurrent = getPageCurrent(request);//获取当前页
		int PageSize = 4;//获取每页的记录数
		PageBean<User> pageBean = userServiceImpl.queryAll_Page_Condition(PageCurrent, PageSize,username,dept_id);
		pageBean.setUrl(getUrl(request));
		request.setAttribute("pb", pageBean);
    	
    	return "f:/admin/emp_find_condition.jsp";
    }
}
